package com.guigu.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.DesignModuleDto;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.DesignModule;
import com.guigu.erp.pojo.DesignModuleDetails;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.DesignModuleDetailsService;
import com.guigu.erp.service.DesignModuleService;
import com.guigu.erp.utils.NumberUtils;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignModuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/design/module")
public class DesignModuleController {
    @Autowired
    private DesignModuleService designModuleService;

    @Autowired
    private NumberUtils numberUtils;

    @Autowired
    private DesignFileService designFileService;

    @Autowired
    private DesignModuleDetailsService designModuleDetailsService;

    //添加物料表，物料详情表，修改产品档案表的请求
    @RequestMapping("/addModule")
    @ResponseBody
    public boolean addModule(@RequestBody DesignModuleDto designModuleDto) {
        // 生成物料设计单编号
        String designId = numberUtils.getTypeNumber("200");
        designModuleDto.setDesignId(designId);

        // 设置物料总成本
        BigDecimal sum = new BigDecimal(0);

        List<DesignModuleDetails> details = designModuleDto.getDetails();

        for (int i = 0; i < details.size(); i++) {
            BigDecimal temp = details.get(i).getSubtotal();
            sum = sum.add(temp);
        }

        designModuleDto.setCostPriceSum(sum);

        // 设置审核标志 S001-0 等待审核
        designModuleDto.setCheckTag("S001-0");

        // 设置变更标志 B002-0 未变更
        designModuleDto.setChangeTag("B002-0");

        // 设置当前物料设计单的分类编号
        // 获取产品编号
        String productId = designModuleDto.getProductId();
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", productId);
        DesignFile designFile = designFileService.getOne(wrapper);
        if (designFile != null) {
            designModuleDto.setFirstKindId(designFile.getFirstKindId());
            designModuleDto.setFirstKindName(designFile.getFirstKindName());
            designModuleDto.setSecondKindId(designFile.getSecondKindId());
            designModuleDto.setSecondKindName(designFile.getSecondKindName());
            designModuleDto.setThirdKindId(designFile.getThirdKindId());
            designModuleDto.setThirdKindName(designFile.getThirdKindName());
        }

        // 调用业务层方法，
        boolean flag = designModuleService.addMaterial(designModuleDto);

        return flag;
    }

    //查询审核的物料设计单
    @RequestMapping("/auditList")
    @ResponseBody
    public PageUtils<DesignModule> auditList(Integer pageNo, Integer pageSize) {
        QueryWrapper<DesignModule> wrapper = new QueryWrapper<>();
        //S001-0: 等待审核
        wrapper.eq("CHECK_TAG", "S001-0");
        //使用分页工具
        PageHelper.startPage(pageNo, pageSize);
        List<DesignModule> list = designModuleService.list(wrapper);
        PageInfo<DesignModule> pageInfo = new PageInfo<>(list);
        PageUtils<DesignModule> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }

    //根据id查询物料设计单的数据
    @RequestMapping("/getAuditById")
    @ResponseBody
    public DesignModule getAuditById(Integer id){
        DesignModule module = designModuleService.getById(id);
        return module;
    }

    //根据物料设计单id查询物料明细
    @RequestMapping("/getMaterialDetailsById")
    @ResponseBody
    public List<DesignModuleDetails> getMaterialDetailsById(Integer id){
        QueryWrapper<DesignModuleDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID",id);
        List<DesignModuleDetails> moduleDetails = designModuleDetailsService.list(wrapper);
        return moduleDetails;
    }

    //修改物料设计单的信息
    @RequestMapping("/updateAudit")
    @ResponseBody
    public boolean updateAudit(DesignModule designModule){
        boolean flag = designModuleService.updateById(designModule);
        return flag;
    }

    //查看所有物料设计单(带条件)
    @RequestMapping("/auditInfo")
    @ResponseBody
    public PageUtils<DesignModule> auditInfo(DesignModuleVo vo,Integer pageNo,Integer pageSize){
        PageUtils<DesignModule> pageUtils = designModuleService.auditPage(vo, pageNo, pageSize);
        return pageUtils;
    }
}
